import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast.js';
// 获取应用实例
const app = getApp()
let base64Str = ""
Page({
  data: {
    showfir: true,
    userInfo: {
      gender: 0,
    },
    imageSrc:"",
    face_list:"",
    ratio:1,
    showbtn: true,
    msg: [
      '马**正在上传个人形象检测',
      '爱**查看穿搭推荐',
      '成**正在进行身体数据测量'
    ],
    autoplay_two: true,
    autoplay_two: true,
    vertical_two: true,
    interval_two: 200,
    duration_two: 500,
    interval: 5e3,
    duration: 500,
  },
  // 前往上传图片页面
  gotoPage() {
    wx.navigateTo({
      url: '/pages/view/ai-view/pre-index/pre-index',
    })
  },
  // 关闭弹框
  hideClose() {
    this.setData({
      showfir: false
    })
  },
  handleClick:function(){
    wx.chooseImage({
      count: 1,
      sizeType:["compressed","original"],
      sourceType:["album","camera"],
      success:(res)=>{
        // 把图片路径展示到页面上
          this.setData({
            imageSrc:res.tempFilePaths[0]
          })
          wx.getImageInfo({
            src: res.tempFilePaths[0],
            success:(res)=>{
              console.log(300/res.width);
              this.setData({
                ratio:300/res.width
              })
            }
          })
        // 获取base64 编码
        this.getBASE64String(res.tempFilePaths[0]);
        this.getAccessToken();
      }
    })
  },
  // 获取百度智能云的 access_token
  getAccessToken:function(){
    wx.request({
      method:"get",
      url: 'https://aip.baidubce.com/oauth/2.0/token',
      data:{
        'grant_type': 'client_credentials',
        'client_id': 'stRN7mbhxq1nFuoMbSqlxjZG',
        'client_secret': 'ivzLFnITb6Np3QQl2ptyqTlDsBdPWIjm'
      },
      success:res=>{
        this.getFaceInfo(res.data.access_token);
      },
      fail:err=>{
        console.log(err);
      }
    })
  },

  getFaceInfo(token){
    wx.request({
      url:"https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token="+token,
      method:"POST",
      header:{
        "Content-Type":"application/json"
      },
      data:{
        image:base64Str,
        image_type:"BASE64",
        face_field:"beauty,age,expression,face_shape,gender,glasses,landmark,landmark150,quality,eye_status,emotion,face_type,mask,spoofing"
      },
      dataType:"json",
      success:(res)=>{
        console.log(res);
        if(res.data.error_code == 0){
          console.log("ojb=>",res.data.result.face_list[0]);
            this.setData({
              face_list:res.data.result.face_list[0]
            })
        }else{
          console.log("程序报错，错误码为 =>",data.error_code);
          this.setData({
            obj:null
          })
        }
      }

    })
  },

  // 获取BASE64 String
  getBASE64String:function(filePath){
    let file = wx.getFileSystemManager();
    file.readFile({
      filePath,
      encoding:"base64",
      success:(res)=>{
        // console.log(res.data);
        base64Str = res.data;
      }
    })
  },
  toMeasureDetail() {
    if (!this.data.gender) {
      wx.showToast({
        icon: 'error',
        title: '请先选择性别'
      })
      return
    }
    if (!this.data.height) {
      wx.showToast({
        icon: 'error',
        title: '请先选择身高'
      })
      return
    }
    if (!this.data.weight) {
      wx.showToast({
        icon: 'error',
        title: '请先选择体重'
      })
      return
    }
    const params = `radio=${this.data.radio}&height=${this.data.height}&weight=${this.data.weight}&bust=${this.data.bust}&waist=${this.data.waist}&hip=${this.data.hip}`
    wx.navigateTo({
      url: '/pages/view/ai-view/measure-detail/measure-detail?' + params,
    })
  },
  changeUserGender() {
    wx.hideTabBar()
    this.setData({
      show: true,
      popTitle: '更改性别',
      // nickName: this.data.userNickname,
      // radio: String(this.data.userGender)
    })
  },
  onClose() {
    const that = this
    if (this.data.popTitle === '更换绑定手机号') {
      // that.toChangeAvatar()
    } else if (this.data.popTitle === '更改昵称') {
      console.log(that.data.nickName)
      that.toEditUserInfo()
    } else if (this.data.popTitle === '更改性别') {
      console.log(that.data.radio)
      if(that.data.radio === '1') {
        this.setData({
          gender: '男'
        })
      } else {
        this.setData({
          gender: '女'
        })
      }
      // that.toEditUserInfo()
    }
    this.setData({
      show: false
    })
    setTimeout(()=> {
      wx.showTabBar()
    }, 300)
  },
  onClick(event) {
    this.setData({
      radio: event.currentTarget.dataset.name
    });
    this.onClose()
  },
  toEditUserInfo() {
    console.log('编辑用户信息')
  },
  onLoad() {
    // if (wx.getUserProfile) {
    //   this.setData({
    //     canIUseGetUserProfile: true,
    //     showSubscribe: true
    //   })
    // }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})